package twoPointer;

public class saveWater {

    public static void main(String[] args) {

    }

    public int trap(int[] height){
        if (height == null|| height.length==0) {
            return 0;
        }
        int length=height.length;
        int left=0;
        int right=length-1;
        int leftHeight=height[left];
        int rightHeight=height[right];
        int sum=0;
        while(left<right){
            if(leftHeight<rightHeight){
                if(leftHeight>height[left+1]){
                    sum+=leftHeight-height[left+1];
                }else {
                    leftHeight=height[left+1];
                }
                left++;
            }else {
                if(rightHeight>height[right-1]){
                    sum+=rightHeight-height[right-1];
                }else{
                    rightHeight=height[right-1];
                }
                right--;
            }
        }
        return sum;
    }
}
